
//
//  ShouldReceiveDetailViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/14.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ShouldReceiveDetailViewController: PagedViewController {
    
    var id = ""
    
    let approvalBtn = UIButton.init(type: .system)
    
    @objc func approvalAction(btn: UIButton)->Void{
        let suggestion = AddUrgeViewController()
        suggestion.id = self.id
        suggestion.delegate = self
        self.present(vc: suggestion)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        approvalBtn.setTitle("更新催收动态", for: .normal)
        
        approvalBtn.setTitleColor(UIColor.white, for: .normal)
        
        approvalBtn.setTitleColor(UIColor.hex("999999"), for: .disabled)
        
        approvalBtn.backgroundColor = UIColor.mainColor
        
        approvalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        approvalBtn.addTarget(self, action: #selector(approvalAction(btn:)), for: .touchUpInside)
        
        self.view.addSubview(approvalBtn)
        
        self.load(with: names[0])
        
        self.load(with: names[1])
        
        self.load(with: names[2])
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        approvalBtn.frame = CGRect(origin: CGPoint(x: 0.0, y: self.view.height - 55), size: CGSize(width: self.view.width, height: 55))
        page.view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: trayView.bottom), size: CGSize(width: self.view.width, height: self.view.height - trayView.height - approvalBtn.height))
    }
    
    override func configTitles() -> Array<String> {
        return ["合同信息","应收账款信息","应收账款动态"]
    }
    
    override func configViewControllers() -> [UIViewController] {
        return [TitleContentTableViewController(),TitleContentTableViewController(),IncomeDynamicTableViewController()]
    }
    
    override var names: Array<String>{
        get{
            return ["ContractInfo","CreditInfo","CreditDynamic","UPDate"]
        }
    }
    
    override func path(for name: String) -> String {
        switch name {
        case names[0]:
            return ShouldIncomeDetail
        case names[1]:
            return ShouldIncomeInfo
        case names[2]:
            return ShouldIncomeDynamic
        case names[3]:
            return UpdateIncomeDynammic
        default:
            return ""
        }
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["id": self.id]
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let data = json.contents?["data"]?.contents {
                var contents: [Dictionary<String,String?>] = []
                contents.append(["合同名称:": data["contractName"]?.rawString()])
                contents.append(["合同编号:": data["contractNum"]?.rawString()])
                contents.append(["K2编号:": data["k2num"]?.rawString()])
                let signmoney = (data["signmoney"]?.int ?? 0) / 100
                contents.append(["合同签订金额(元):": String(signmoney).rmbformat])
                let finalAmount = (data["finalAmount"]?.int ?? 0) / 100
                contents.append(["合同决算金额(元):": String(finalAmount).rmbformat])
                contents.append(["签订日期:": data["signdata"]?.rawString()])
                contents.append(["客户单位:": data["customerConpany"]?.rawString()])
                contents.append(["收入来源:": data["sourcedatatmp"]?.rawString()])
                contents.append(["收款方式:": data["paymentMethodName"]?.rawString()])
                contents.append(["收款方式说明:": data["explain"]?.rawString()])
                let invoiceMoney = (data["invoiceMoney"]?.int ?? 0) / 100
                contents.append(["累计开票金额(元):": String(invoiceMoney).rmbformat])
                let backMoney = (data["backMoney"]?.int ?? 0) / 100
                contents.append(["累计回款金额(元):": String(backMoney).rmbformat])
                contents.append(["合同开始日期:": data["statedata"]?.rawString()])
                contents.append(["合同结束日期:": data["enddata"]?.rawString()])
                let first = self.ctls[0] as! TitleContentTableViewController
                first.source = contents
                first.tableView.reloadData()
            }
        }
        
        func info(json: JSON)->Void{
            if let data = json.contents?["data"]?.contents {
                var contents: [Dictionary<String,String?>] = []
                let remaining = (data["openingbalance"]?.int ?? 0)
                contents.append(["年初余额(元):": String(remaining / 100).rmbformat])
                let ticket = data["yearInvoiceMoney"]?.int ?? 0
                contents.append(["本年开票金额(元):": String(ticket / 100).rmbformat])
                let back = data["yearBackMoney"]?.int ?? 0
                contents.append(["本年回款金额(元):": String(back / 100).rmbformat])
                contents.append(["应收账款比例含税:": data["accReceRate"]?.rawString()])
                contents.append(["合同签署情况:": data["contractState"]?.rawString()])
                let forticket = data["advanceInvoice"]?.int ?? 0
                contents.append(["提前开票金额(元):": String(forticket / 100).rmbformat])
                let delayticket = data["delayInvoice"]?.int ?? 0
                contents.append(["延迟开票金额(元):": String(delayticket / 100).rmbformat])
                let conditionalticket = data["canReceive"]?.int ?? 0
                contents.append(["具备条件收款(元):": String(conditionalticket / 100).rmbformat])
                let notconditionticket = data["cannotReceive"]?.int ?? 0
                contents.append(["不具备条件收款(元):": String(notconditionticket / 100).rmbformat])
                let delaynotincome = data["unreceive"]?.int ?? 0
                contents.append(["逾期未收款金额(元):": String(delaynotincome / 100).rmbformat])
                let final = data["unexpiredPremium"]?.int ?? 0
                contents.append(["未到期质保金金额(元):": String(final / 100).rmbformat])
                let vc = self.ctls[1] as! TitleContentTableViewController
                vc.source = contents
                vc.tableView.reloadData()
            }
        }
        
        func dynamics(json: JSON)->Void{
            print(json)
            if let data = json.contents?["data"]?.contents {
                var contents: [[Dictionary<String,String?>]] = []
                let responsiblePersonName = data["responsiblePersonName"]?.rawString()
                let dicperson = ["催收责任人:": responsiblePersonName]
                let noreceiveInfo = ["三年以上未回款说明:": data["noreceiveInfo"]?.rawString()]
                let amount = (data["baddebtsAmount"]?.int ?? 0) / 100
                let amountdic = ["申请核销坏账金额(元):": String(amount).rmbformat]
                let amountReason = data["amountReason"]?.rawString()
                let dic = ["申请核销坏账原因:":amountReason]
                let arr = [dicperson,noreceiveInfo,amountdic,dic]
                contents.append(arr)
                if let dynamics = data["DynamicCreditList"]?.array {
                    for item in dynamics {
                        let dynamicname = item["feedback"].rawString() ?? ""
                        let createTime = item.contents?["createTime"]?.double ?? 0.0
                        let date = Date.init(timeIntervalSince1970: createTime / 1000).dateLineStr
                        let element = [["催收动态":dynamicname],["更新日期": date]]
                        contents.append(element)
                    }
                }
                let vc = self.ctls[2] as! IncomeDynamicTableViewController
                vc.source = contents
                vc.tableView.reloadData()
            }
        }
        switch name {
        case names[0]:
            return suc
        case names[1]:
            return info
        case names[2]:
            return dynamics
        default:
            return suc
        }
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
}
